package com.diandi.signature.manager.impl.CAInfo;


import com.diandi.signature.bean.cainfo.SignatureCAInfo;
import com.diandi.signature.bean.pkgmsg.SignatureBusinessPkgmsg;
import com.diandi.signature.bean.pkgmsg.SignaturePlatformPkgMsg;
import com.diandi.signature.enums.platfrom.PkgMsgStatus;
import com.diandi.signature.manager.CAInfo.SignatureCAManager;
import com.diandi.signature.mapper.*;
import com.diandi.signature.utils.PackageParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 *
* @author mwz
* @create 2017-08-23 13:47
* @version V1.0.0
* @description description
*/
@Component
public class SignatureCAManagerImpl implements SignatureCAManager {


    /**
     * CA认证
     */
    @Autowired
    private SignatureCAInfoMapper signatureCAInfoMapper;

    /**
     * 签章处理过程主表
     */
    @Autowired
    private SignatureProcessLogMapper signatureProcessLogMapper;

    /**
     * 签章处理过程子表
     */
    @Autowired
    private SignatureProcessDetailMapper signatureProcessDetailMapper;

    @Autowired
    private SignaturePlatformPkgmsgMapper signaturePlatformPkgmsgMapper;
    @Autowired
    private SignatureBusinessPkgmsgMapper signatureBusinessPkgmsgMapper;

    /**
     * 查询CA认证消息
     * @param paramMap
     * @return
     */
    @Override
    public SignatureCAInfo getSignatureCAInfoByCode(Map<String, Object> paramMap) {
        return signatureCAInfoMapper.getSignatureCAInfoByCode(paramMap);
    }

    @Transactional
    @Override
    public SignatureCAInfo insertCAInfo(Map<String, String> params) throws Exception {
        /**
         * 1.1 构建CA信息
         */
        SignatureCAInfo caInfo = new SignatureCAInfo();
        caInfo.setUserId( Long.valueOf(params.get("userId")) );
        caInfo.setPassCode( params.get("passCode"));
        caInfo.setSignatureCustomerId( params.get("customerId") );
        caInfo.setUserName( params.get("customerName"));
        caInfo.setPhone( params.get("phone"));
        if (StringUtils.isNotBlank(params.get("email"))){
            caInfo.setEmail( params.get("email"));
        }
        caInfo.setStatus(Integer.valueOf(params.get("status")));
        signatureCAInfoMapper.insertCAInfo( caInfo );

        /**
         * 2.1 记录中间状态表, 封装业务ID及交易流水号
         */
//        SignatureProcessLog signatureRecordLog = PackageParams.getSignatureProcessPkgMsg(params);
//        signatureProcessLogMapper.insertSignatureProcessLog(signatureRecordLog);

        //保存中间处理明细表
//        SignatureProcessDetail payProcessDetail = PackageParams.getSignatureProcessDetailPkgMsg(signatureRecordLog);
//        signatureProcessDetailMapper.insertSignatureProcessDetail(payProcessDetail);

        /**
         * 3.1 保存平台报文表
         */
//        SignatureBusinessPkgmsg signatureBusinessPkgmsg = PackageParams.getBusinessPkgmsg(params);
//        signatureBusinessPkgmsgMapper.insertSignatureBusinessPkgmsg(signatureBusinessPkgmsg);
        return caInfo;
    }

    /**
     * 完成CA认证后更新customerId和状态
     * @param paramMap
     * @return
     */
    @Override
    public Integer updateCustomerIdAndStatus(Map<String, Object> paramMap) {
        return signatureCAInfoMapper.updateCustomerIdAndStatus(paramMap);
    }

    @Transactional
    @Override
    public boolean insertRcvPkgMsgData(  Map<String, String> params) throws Exception {

        /**
         * 1.1 记录业务端报文
         */
        SignatureBusinessPkgmsg signatureBusinessPkgmsg =  PackageParams.getSignatureBusinessPkgmsg( params );
        signatureBusinessPkgmsgMapper.insertSignatureBusinessPkgmsg( signatureBusinessPkgmsg );
        return true;

    }

    @Transactional
    @Override
    public boolean updateRcvPkgMsgData( Map<String, String> requestMap,Map<String, String> rtnMap) throws Exception {

        /**
         * 1.1 记录业务端报文
         */
        SignatureBusinessPkgmsg signatureBusinessPkgmsg =  PackageParams.getSignatureBusinessPkgmsg( requestMap );
        signatureBusinessPkgmsg.setReturnMsg( rtnMap.toString() );
        if (rtnMap.containsKey("code") && "1000".equals(rtnMap.get("code"))){
            signatureBusinessPkgmsg.setStatus(PkgMsgStatus.成功.getStatus());
        } else {
            signatureBusinessPkgmsg.setStatus(PkgMsgStatus.处理失败.getStatus());
        }
        signatureBusinessPkgmsgMapper.updateSignatureBusinessPkgmsg( signatureBusinessPkgmsg );
        return true;

    }

    @Transactional
    @Override
    public boolean insertRtnPkgMsgData( Map<String, String> params) throws Exception {
        SignaturePlatformPkgMsg signaturePlatformPkgMsg =  PackageParams.getSignaturePlatformPkgMsg( params );
        signaturePlatformPkgMsg.setRequestMsg(params.toString() );
        signaturePlatformPkgmsgMapper.insertSignaturePlatformPkgmsg( signaturePlatformPkgMsg );
        return true;
    }

    @Transactional
    @Override
    public boolean updateRtnPkgMsgData(   Map<String, String> requestMap, Map<String, String> params) throws Exception {
        SignaturePlatformPkgMsg signaturePlatformPkgMsg =  PackageParams.getSignaturePlatformPkgMsg( params );
        signaturePlatformPkgMsg.setReturnMsg(params.toString() );
        if (params.containsKey("code") && "1000".equals(params.get("code"))){
            signaturePlatformPkgMsg.setStatus(PkgMsgStatus.成功.getStatus());
        } else {
            signaturePlatformPkgMsg.setStatus(PkgMsgStatus.处理失败.getStatus());
        }
        signaturePlatformPkgmsgMapper.updateSignaturePlatformPkgMsg( signaturePlatformPkgMsg );
        return true;
    }

    @Override
    public SignatureCAInfo getSignatureCAInfo(String userId, String passCode) {
        Map<String, Object> queryCa = new HashMap<String, Object>();
        queryCa.put("userId", userId);
        queryCa.put("passCode", passCode);
        queryCa.put("status", com.diandi.signature.enums.fdd.CAInfoStatus.申请成功.getType());
        return signatureCAInfoMapper.getSignatureCAInfoByCode(queryCa);
    }
}
